package com.shopping.mall.core.module.favorite.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopping.mall.auth.utils.UserUtils;
import com.shopping.mall.core.module.favorite.entity.Favorite;
import com.shopping.mall.core.module.favorite.mapper.FavoriteMapper;
import com.shopping.mall.core.module.favorite.service.FavoriteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopping.mall.base.http.ResponseState;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.base.util.WrapperUtils;
import com.shopping.mall.core.module.goods.entity.Goods;
import com.shopping.mall.core.module.goods.mapper.GoodsMapper;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * <p>
 * Favorite服务实现类
 * </p>
 *
 * @author zzy
 * @since 2021-08-15
 */
@Validated
@Service
public class FavoriteServiceImpl extends ServiceImpl<FavoriteMapper, Favorite> implements FavoriteService {

    @Autowired
    private FavoriteMapper favoriteMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private Logger logger;

    /**
     * 收藏夹添加商品
     *
     * @param goodsId: 商品id
     * @return Result<Object>
     */
    @Override
    public Result<Object> add(String goodsId) {
        final String userId = UserUtils.getLoginUserId();
        Favorite favorite = new Favorite();
        favorite.setUserId(userId);
        favorite.setGoodsId(goodsId);
        favoriteMapper.insert(favorite);
        return Result.OK();
    }

    /**
     * 查看收藏夹(分页)
     *
     * @param current：页码
     * @param size：每页条数
     * @return
     */
    @Override
    public Result<Object> favoritePage(Integer current, Integer size) {
        final String userId = UserUtils.getLoginUserId();
        QueryWrapper<Favorite> favoriteQueryWrapper = new QueryWrapper<>();
        favoriteQueryWrapper.eq("user_id", userId);
        final List<Favorite> favorites = favoriteMapper.selectList(favoriteQueryWrapper);
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        goodsQueryWrapper.select("id",
                "name",
                "less_intro",
                "main_image",
                "price",
                "current_price").eq("status", 1);//正常售卖
        goodsQueryWrapper.and(wrapper -> favorites.stream().map(Favorite::getGoodsId).forEach(id -> wrapper.or().eq("id", id)));
        Page<Goods> page = new Page<>(current, size);
        final IPage<Goods> goodsIPage = goodsMapper.selectPage(page, goodsQueryWrapper);
        final List<Goods> records = goodsIPage.getRecords();
        records.forEach(goods -> goods.setFavorite(true));
        return Result.OK(goodsIPage);
    }

    /**
     * 查看收藏夹(不分页)
     *
     * @return
     */
    @Override
    public Result<Object> favorite() {
        final String userId = UserUtils.getLoginUserId();
        QueryWrapper<Favorite> favoriteQueryWrapper = new QueryWrapper<>();
        favoriteQueryWrapper.eq("user_id", userId);
        final List<Favorite> favorites = favoriteMapper.selectList(favoriteQueryWrapper);
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        goodsQueryWrapper.select("id",
                "name",
                "less_intro",
                "main_image",
                "price",
                "current_price").eq("status", 1);//正常售卖
        goodsQueryWrapper.and(wrapper -> favorites.stream().map(Favorite::getGoodsId).forEach(id -> wrapper.or().eq("id", id)));
        final List<Goods> goods = goodsMapper.selectList(goodsQueryWrapper);
        return Result.OK(goods);
    }

    /**
     * 取消收藏
     *
     * @param goodsId
     * @return
     */
    @Override
    public Result<Object> delete(String goodsId) {
        final String userId = UserUtils.getLoginUserId();
        QueryWrapper<Favorite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("goods_id", goodsId);
        final int i = favoriteMapper.delete(queryWrapper);
        logger.info("有{}行被删除", i);
        return Result.OK();
    }

    /**
     * 分页查询
     *
     * @param current：页码
     * @param size：每页条数
     * @param favorite：筛选条件
     * @return IPage<Favorite>
     */
    @Override
    public IPage<Favorite> getFavoritePage(Integer current, Integer size, Favorite favorite) {
        QueryWrapper<Favorite> queryWrapper = WrapperUtils.queryWrapper(favorite);
        Page<Favorite> favoritePage = new Page<>(current, size);
        IPage<Favorite> favoriteIPage = favoriteMapper.selectPage(favoritePage, queryWrapper);
        return favoriteIPage;
    }

    /**
     * 查询所有
     *
     * @param favorite：筛选条件
     * @return List<Favorite>
     */
    @Override
    public List<Favorite> getAll(Favorite favorite) {
        QueryWrapper<Favorite> queryWrapper = WrapperUtils.queryWrapper(favorite);
        return favoriteMapper.selectList(queryWrapper);
    }

    /**
     * 通过id删除
     *
     * @param id：favoriteId
     * @return Result<Object>
     */
    @Override
    public Result<Object> deleteById(String id) {
        Favorite favorite = favoriteMapper.selectById(id);
        if (favorite == null) {
            return Result.error(ResponseState.TARGET_NOT_EXIST.getValue(), ResponseState.TARGET_NOT_EXIST.getMessage());
        }
        favoriteMapper.deleteById(id);
        return Result.OK();
    }

    /**
     * 批量删除
     *
     * @param ids：id列表
     * @return Result<Object>
     */
    @Override
    public Result<Object> batchDelete(List<String> ids) {
        UpdateWrapper<Favorite> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids);
        favoriteMapper.delete(updateWrapper);
        return Result.OK();
    }
}